import {
  getSongDetail,
  getSongLyric
} from '@/services/song-api'
import { formatLyric } from '@/utils/format'
import * as actionTypes from './constants'

const changeCurrentSongDetailAction = res => ({
  type: actionTypes.CHANGE_CURRENT_SONG_DETAIL,
  currentSongDetail:res
})
const changeCurrentIndexAction = currentSongIndex => ({
  type: actionTypes.CHANGE_CURRENT_INDEX,
  currentSongIndex
})
const changeSongListAction = songList => ({
  type: actionTypes.CHANGE_SONG_LIST,
  songList
})
const changeSongLyricAction = songLyric => ({
  type: actionTypes.CHANGE_SONG_LYRIC,
  songLyric
})
export const changeCurrentSongAction = next => {
  return async (dispatch, getState) => {
    let currentSongIndex = getState().getIn(['player', 'currentSongIndex'])  
    const howPlay = getState().getIn(['player','howPlay'])
    const songList = getState().getIn(['player', 'songList'])
    switch (howPlay) {
      case 2:
        let randomIndex = Math.floor(Math.random() * songList.length)
        while (currentSongIndex === randomIndex) {
          randomIndex = Math.floor(Math.random() * songList.length)      
        }
        currentSongIndex = randomIndex
        break;
      default:
        if (next === 1) {
          currentSongIndex += 1
          if (currentSongIndex >= songList.length) {
            currentSongIndex = 0
          }
        } else {
          currentSongIndex -= 1
          if (currentSongIndex < 0) {
            currentSongIndex = songList.length - 1
          }
        }
    }
    

    dispatch(changeCurrentIndexAction(currentSongIndex))
    dispatch(changeCurrentSongDetailAction(songList[currentSongIndex]))
    const res = await getSongLyric(songList[currentSongIndex].id)
    dispatch(changeSongLyricAction(formatLyric(res.lrc.lyric)))

  
  }
}
export const changeHowPlayAction = howPlay => ({
  type: actionTypes.CHANGE_HOW_PLAY_ACTION,
  howPlay
})
const changeCurrentSongLyricIndexAction = currentSongLyricIndex => ({
  type: actionTypes.CAHENG_CURRENT_SONG_LYRIC_INDEX,
  currentSongLyricIndex
})
export const getCurrentSongLyricIndexAction = currentSongLyricIndex => {
  return dispatch => {
    dispatch(changeCurrentSongLyricIndexAction(currentSongLyricIndex))
  }
}
export const getSongDetailAction = ids => {
  return async (dispatch, getState) => {
    const songList = getState().getIn(['player', 'songList'])
    console.log(songList);
    const currentIndex = songList.findIndex(song => song.id === ids)
    if (currentIndex !== -1) {
      //重复歌曲
      dispatch(changeCurrentIndexAction(currentIndex))
    } else {
      //新增歌曲
      const res = await getSongDetail(ids)
      const song = res.songs[0]
      const newSongList = [song, ...songList]
      const lyr_res = await getSongLyric(ids)

      dispatch(changeSongListAction(newSongList))
      dispatch(changeCurrentIndexAction(0))
      dispatch(changeCurrentSongDetailAction(song))
      dispatch(changeSongLyricAction(formatLyric(lyr_res.lrc.lyric)))
      
    }
  }
}
// export const getSongLyricAction = id => {
//   return async dispatch => {
//     const res = await getSongLyric(id)
//     dispatch(changeSongLyric(formatLyric(res.lyr)))
//   }
// }

